package com.asion.qkan.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;

import com.asion.qkan.R;
import com.asion.qkan.adapter.CategoryAdapter;
import com.asion.qkan.entry.RecommendEntry;
import com.asion.qkan.ui.contract.FindContract;
import com.asion.qkan.ui.model.FindModel;
import com.asion.qkan.ui.presenter.FindPresenter;
import com.asion.qkan.ui.util.MyDerocation;
import com.asion.qkan.ui.util.ToastUtil;
import com.asion.qkan.ui.view.AllActivity;

import butterknife.BindView;

import static com.asion.qkan.jiguangtuisong.ExampleApplication.context;

/**
 * Created by gino on 16-10-13.
 */

public class RecommedFragment extends BaseFragment<FindPresenter, FindModel> implements FindContract.View, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.recommend_recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    private CategoryAdapter adapter;

    @Override
    protected int getLayoutResource() {
        return R.layout.recommed_fragment;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
        setDatas();
    }

    private void setDatas() {
        mPresenter.setRecommendDatas(62, 0);
        mPresenter.setRecommendDatas(63, 1);
        new Handler().postDelayed(() -> {
            mPresenter.setRecommendDatas(74, 2);
            mPresenter.setRecommendDatas(53, 3);
            mPresenter.setRecommendDatas(91, 4);
            mPresenter.setRecommendDatas(72, 5);
            mPresenter.setRecommendDatas(73, 6);
        }, 500);
    }

    @Override
    protected void initView() {
        adapter = new CategoryAdapter(getContext());
        recyclerView.setAdapter(adapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(new MyDerocation(context, LinearLayout.HORIZONTAL));
        adapter.setonViewpagerItemListener(bean -> {
            Bundle bundble = new Bundle();
            bundble.putString("id", String.valueOf(bean.getId()));
            bundble.putString("title",bean.getTitle());
            bundble.putString("vertical_image_url",bean.getVertical_image_url());
            AllActivity.startAction(getActivity(),bundble);
        });
        adapter.setOnItemClickListener(bean -> {
            Bundle bundble = new Bundle();
            bundble.putString("id", String.valueOf(bean.getId()));
            bundble.putString("title",bean.getTitle());
            bundble.putString("vertical_image_url",bean.getVertical_image_url());
            AllActivity.startAction(getActivity(),bundble);
        });
        refresh.setOnRefreshListener(this);
    }

    @Override
    public void showDataSuccess(RecommendEntry recommendEntry) {

    }

    @Override
    public void showDataError(Throwable throwable) {
        ToastUtil.showShort("请求失败");
    }

    @Override
    public void showDatas(RecommendEntry recommendEntry, int type) {
        int itemCount = adapter.getItemCount();
        String title = null;
        switch (type) {
            case 0:
                break;
            case 1:
                title = "热门经典";
                break;
            case 2:
                title = "少年热血";
                break;
            case 3:
                title = "轻松爆笑";
                break;
            case 4:
                title = "纯爱少女";
                break;
            case 5:
                title = "耽美bl";
                break;
            case 6:
                title = "绝美古风";
                break;
        }
        adapter.addData(recommendEntry.getData(), itemCount, title);
    }

    @Override
    public void onRefresh() {
        new Handler().postDelayed(() -> {
            adapter.removeAll();
            setDatas();
            refresh.setRefreshing(false);
        }, 3000);
    }
}
